<?php
/**
 * Created by PhpStorm.
 * Author: Shadow
 * Date: 2022/6/5
 * Time: 1:56 下午
 * description: CartBusiness.php
 * 购物车业务逻辑类
 */
namespace app\home\business;

use think\facade\Session;

class CartBusiness {
    /**
     * @param $skuInfo
     * @param $number
     */
    public static function addCart($skuInfo,$number){
        if (Session::has('userInfo')) {
            # 登录、可以直接入库
            # 入库之前、需要进行判断、判断该用户是否添加过相同的商品、有、数量增加、没有、新增
            $user_id = session('userInfo.id');
            $where = [
                ['user_id','=',$user_id],
                ['goods_id','=',$skuInfo['goods_id']],
                ['spec_goods_id','=', $skuInfo['id']],
            ];
            $is_add = \app\common\model\Cart::where($where)->find();
            if ($is_add) {
                # 存在相同的商品
                $result = \app\common\model\Cart::where($where)->update(['number' => $is_add->number + $number]);
            } else {
                # 不存在相同的商品
                $data = [
                    'user_id' => $user_id,
                    'goods_id' => $skuInfo['goods_id'],
                    'number' => $number,
                    'spec_goods_id' => $skuInfo['id'],
                    'is_selected' => 1,//购物选中状态
                ];

                $result = \app\common\model\Cart::create($data);
            }

            if (!$result) {
                abort(2001,"添加失败");
            }
        } else {
            # 未登录、加入购物车、存到cookie、可以跳转登录
            $data = [
                'goods_id' => $skuInfo['goods_id'],
                'spec_goods_id' => $skuInfo['id'],
                'number' => $number,
                'is_selected' => 1,
            ];
            cookie('cart_data',json_encode($data));
        }
    }

    /**
     * 购物车列表
     */
    public static function getCart(){
        // 判断用户是否登录
        if (Session::has('userInfo')) {
            # 查询数据库、展示数据
            $data = \app\common\model\Cart::with(['spu','sku'])
                ->where('user_id','=',session('userInfo.id'))
                ->visible([
                    'spu' => ['goods_name','goods_logo'],
                    'sku' => ['price','value_names']
                ])
                ->select()->toArray();
            return $data;
        } else {
            # 获取cookie数据、进行展示
        }

    }

    /**
     * 修改购买数量
     * @param $id
     * @param $number
     */
    public static function changeNumber($id,$number){
        if (\session("?userInfo")) {
            # 用户登录了、修改数据库之前、也可以判断一下库存

            $result = \app\common\model\Cart::where('id','=',$id)->update(['number' => $number]);

            if (!$result) {
                abort(2001,"库存不足");
            }

        } else {
            # 修改cookie
            $cart_data = json_decode(cookie('cart_data'));

        }

    }

    /**
     * 修改选择状态
     * @param $id
     * @param $is_selected
     */
    public static function changeSelected($id,$is_selected){
        if (\session("?userInfo")) {
            # 用户登录了、修改数据库之前、也可以判断一下库存

            $result = \app\common\model\Cart::where('id','=',$id)->update(['is_selected' => $is_selected]);

            if (!$result) {
                abort(2001,"库存不足");
            }

        } else {
            # 修改cookie
            $cart_data = json_decode(cookie('cart_data'));

        }

    }

    /**
     * @param $id
     */
    public static function del($id) {
        if (\session("?userInfo")) {
            $result = \app\common\model\Cart::where('id','=',$id)->delete();

            if (!$result) {
                abort(2001,"库存不足");
            }
        } else {

        }
    }

    /**
     * @param $id
     */
    public static function batchDel($id) {
        if (\session("?userInfo")) {
            $result = \app\common\model\Cart::whereIn('id',$id)->delete();

            if (!$result) {
                abort(2001,"库存不足");
            }
        } else {

        }
    }
}